package com.example.unitybletest;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.seventh.blecore.ByteArray;
import com.seventh.blecore.UnityCallBle;
import com.seventh.blecore.Utils;
import com.seventh.blecore.callback.NotifyCallback;
import com.seventh.blecore.callback.ResponseCallback;
import com.seventh.blecore.callback.SearchCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.UUID;

public class MainActivityHandBoom extends AppCompatActivity implements View.OnClickListener {
    private final UUID serviceUUID = UUID.fromString("0000180a-0000-1000-8000-00805f9b34fb");
    private final UUID characterUUID = UUID.fromString("00002ad9-0000-1000-8000-00805f9b34fb");
    private byte[] actionStart = {0x0C, 0x11, 0x09, 0x47, 0x00, 0x00, 0x01, 0x01, (byte) 0x90};
    private byte[] actionPause = {0x0C, 0x11, 0x09, 0x47, 0x00, 0x00, 0x01, 0x00, (byte) 0x8F};
    private byte[] actionClenchHand = {0x0C, 0x11, 0x09, 0x47, 0x01, 0x00, 0x01, 0x01, (byte) 0x8F};
    private byte[] actionOpenHand = {0x0C, 0x11, 0x09, 0x47, 0x01, 0x00, 0x01, 0x02, (byte) 0x8E};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_search).setOnClickListener(this);
        findViewById(R.id.btn_connect).setOnClickListener(this);
        findViewById(R.id.btn_notify).setOnClickListener(this);
        UnityCallBle.getInstance(this).setAndroidTestActivity(this);
        //Utils.d(0x90 + "==" + (byte) 0x90);
        //Utils.d(0x8E + "==" + (byte) 0x8E);
        //Utils.d(0x8F + "==" + (byte) 0x8F);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_search) {
            UnityCallBle.getInstance().searchByName(null, 5000, true, new SearchCallback() {
                @Override
                public void onSearchStarted() {
                    Log.e("print", "onSearchStarted");
                }

                @Override
                public void onDeviceFounded(@NonNull String name, @NonNull String mac, int rssi) {
                    Log.e("print", "onDeviceFounded: name->" + name + " mac->" + mac + " rssi->" + rssi);
                }

                @Override
                public void onSearchStopped(@NonNull String devicesJson, int indexOfDevice) {
                    Utils.d(devicesJson);
                    Log.e("print", "onSearchStopped:" + devicesJson);
                    try {
                        JSONArray ja = new JSONArray(devicesJson);
                        for (int index = 0; index < ja.length(); index++) {
                            JSONObject jo = ja.getJSONObject(index);
                            if (jo.getString("name").contains("SY_C11")) {
                                connect(jo.getString("mac"));
                                break;
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onSearchError(int code, @NonNull String msg) {
                    Log.e("print", "onSearchError: code->" + code + ", msg" + msg);

                }
            });
        } else if (v.getId() == R.id.btn_connect) {
            UnityCallBle.getInstance().write(true, mMac, serviceUUID.toString(), characterUUID.toString(), actionClenchHand, new ResponseCallback() {
                @Override
                public void onResponse(@NonNull String mac, int code, @NonNull String msg) {
                    if (code == 0) {
                        Log.e("print", "writeNoRsp 成功 ");
                    } else {
                        Log.e("print", "writeNoRsp 失败 ");
                    }
                }
            });
        } else if (v.getId() == R.id.btn_notify) {
            UnityCallBle.getInstance().write(true, mMac, serviceUUID.toString(), characterUUID.toString(), actionOpenHand, new ResponseCallback() {
                @Override
                public void onResponse(@NonNull String mac, int code, @NonNull String msg) {
                    if (code == 0) {
                        Log.e("print", "writeNoRsp 成功 ");
                    } else {
                        Log.e("print", "writeNoRsp 失败 ");
                    }
                }
            });
        }
    }

    private String mMac = "F6:0D:0C:5E:A2:2B";

    private void connect(String mac) {
        mMac = mac;
        UnityCallBle.getInstance().connect(mac, new ResponseCallback() {
            @Override
            public void onResponse(@NonNull String mac, int code, @NonNull String msg) {
                Log.e("print", "onConnectResponse: code->" + code + ", msg" + msg);
                if (code == 0) {
                    //MainActivityHandBoom.this.notify(mac);
                    UnityCallBle.getInstance().write(true, mMac, serviceUUID.toString(), characterUUID.toString(), actionStart, new ResponseCallback() {
                        @Override
                        public void onResponse(@NonNull String mac, int code, @NonNull String msg) {
                            if (code == 0) {
                                Log.e("print", "writeNoRsp 成功 ");
                            } else {
                                Log.e("print", "writeNoRsp 失败 ");
                            }
                        }
                    });
                }
            }
        });
    }

    private void notify(String mac) {
        UnityCallBle.getInstance().notify(mac, serviceUUID.toString(), characterUUID.toString(), new NotifyCallback() {
            @Override
            public void onByteArray(String mac, @NonNull ByteArray byteArray) {
                Log.e("print", "onNotifyData:" + Utils.bytesToHex(byteArray.array));
            }

            @Override
            public void onResponse(@NonNull String mac, int code, @NonNull String msg) {
                Log.e("print", "onNotifyResponse: code->" + code + ", msg" + msg);
            }
        });
    }
}